package com.shasselclub.api.weixin.usermanage;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.itit.itf.okhttp.FastHttpClient;
import io.itit.itf.okhttp.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;

/**
 * @description: 通过普通的access_token和openid获取用户的所有信息
 * @author: zhuangxl
 * @since: 21/3/4 19:57
 **/
@Slf4j
public class WxUtil {

    private static final String APPID = "wxed4fb54af80d9c46";
    private static final String APPSECRET = "3a6902bbc276069019831f78b21ac280";

    /**
     * 基础access_token api
     * "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET"
     */
    private static String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";

    /**
     *  获取用户的所有信息
     *  https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
     **/
    private static String ALL_USERINFO_URL = "https://api.weixin.qq.com/cgi-bin/user/info";

    private static String accessToken = "";

    static {
        initToken();
    }


    /**定时刷新access_token**/
    @Scheduled(fixedRate = 60 * 60 * 1000)
    public static void initToken() {

        accessToken = getAccessToken();
        log.info("获取token：{}", accessToken);

    }

    /**
     * @description: 获取普通access_token
     * @author: zhuangxl
     * @since: 21/3/4 20:18
     * @return: java.lang.String
     **/
    public static String getAccessToken() {
        
        String url = WxUtil.ACCESS_TOKEN_URL
                + "&appid="+ WxUtil.APPID
                + "&secret=" + WxUtil.APPSECRET;

        String accessToken = "";
        try {
            Response response = FastHttpClient.get().url(url).build().execute();
            JSONObject jsonObject = JSONObject.parseObject(response.body().string());
            accessToken = jsonObject.get("access_token").toString();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return accessToken;
    }


    /**
     * @description: 通过普通access_token获取用户的所有信息
     * @author: zhuangxl
     * @since: 21/3/4 20:23
     * @param access_token: 
     * @param openid: 
     * @return: com.tnt.usermanage.WxUserinfo
     **/
    public static WxUserinfo getUserinfo(String access_token, String openid) throws Exception{

        String url = WxUtil.ALL_USERINFO_URL
                + "?access_token=" + access_token
                + "&openid=" + openid
                + "&lang=zh_CN";

        Response response = FastHttpClient.get().url(url).build().execute();
        JSONObject jsonObject = JSONObject.parseObject(response.body().string());
        WxUserinfo userinfo = JSON.toJavaObject(jsonObject, WxUserinfo.class);

        return userinfo;

    }

    /**
     * @description: 查询指定openid用户的所有信息
     * @author: zhuangxl
     * @since: 21/3/4 20:32
     * @param openid: 
     * @return: com.tnt.usermanage.WxUserinfo
     **/
    public static WxUserinfo getUserinfo(String openid) throws Exception{

        String url = WxUtil.ALL_USERINFO_URL
                + "?access_token=" + WxUtil.getAccessToken()
                + "&openid=" + openid
                + "&lang=zh_CN";

        Response response = FastHttpClient.get().url(url).build().execute();
        JSONObject jsonObject = JSONObject.parseObject(response.body().string());
        WxUserinfo userinfo = JSON.toJavaObject(jsonObject, WxUserinfo.class);

        return userinfo;

    }

}
